local BackGroundLayer =import(".view.layer.BackGroundLayer")
local AnimationLayer = import(".view.layer.AnimationLayer")
local UiLayer =import(".view.layer.UiLayer")
local scheduler = import("framework.scheduler")

local MainScene = class("MainScene", function()
    return display.newScene("MainScene")
end)

function MainScene:ctor()
 print("MainScene called")
 self._time =0
 self.updateTime =0
 self.isInit =false
 self.schedule=scheduler.scheduleGlobal(handler(self,self.onTiming),Context.interval)
end

function MainScene:init()
 self.bgLayer =BackGroundLayer.new()
 self:addChild(self.bgLayer)
 self.AnimationLayer =AnimationLayer.new()
 self:addChild(self.AnimationLayer)
 self.uiLayer =UiLayer.new()
 self:addChild(self.uiLayer)
 --scheduleGlobal是不能拥有 外部的c++对象引用，即使，它拥有其所属的lua引用。如果要用，必须用到retain()保留下

 self.AnimationLayer:onUpdate()
 self.AnimationLayer:onUpdate() 
 self.AnimationLayer:onUpdate()  
 self:move()


end





--震动
function MainScene:vabration()
  local fudu =20
  local t =0.05
  local left1 =CCMoveTo:create(t,CCPoint(fudu,0))
   local right1 =CCMoveTo:create(t,CCPoint(-fudu,0))
   local top1 =CCMoveTo:create(t,CCPoint(0,fudu))
   local bottom1 =CCMoveTo:create(t,CCPoint(0,-fudu))
   local actions =CCArray:create();
   actions:addObject(left1)
   actions:addObject(right1)
   actions:addObject(top1)
   actions:addObject(bottom1)
   local shakeAction =CCSequence:create(actions)
   self:runAction(shakeAction)
end

function MainScene:move()
   transition.moveBy(self.AnimationLayer, {x = -550, time = 1,onComplete=handler(self,self.move)})
end




function MainScene:onTiming()
   viewController:onTiming()
 
end





function MainScene:passCheck()--是否通过了当前管子
  if viewController:isRunning() and self:isColliWithFish()  then
    print("game over 3")
    viewController:setStateGameOver()
    return
  end
  if viewController:isRunning() then
     self:colliCheckOther()
  end
  if self:passOne() then
    self.uiLayer:changeScore()
    print("pass one")
  end

end


function MainScene:colliCheckOther()
local fishRect =self.uiLayer:getFishRect()
 self.AnimationLayer:colliCheckOther(fishRect)
  if self:checkColliWithDaoJu() then
     print("isColliWithDaoJu")
     self.uiLayer:collWithDaoJu()
  end
end


function MainScene:checkColliWithDaoJu()--碰到一个道具
 local fishRect =self.uiLayer:getFishRect()
 return self.AnimationLayer:checkColliWithDaoJu(fishRect)
end

function MainScene:checkPassByDaoJu() --从道具旁边经过
 local fishRect =self.uiLayer:getFishRect()
 local daoJuRect =self.AnimationLayer:getDaoJuRect()
 if fishRect:getMinX()>daoJuRect:getMaxX() then
  self.AnimationLayer:onDaoJuPass()
 end
end


function MainScene:passOne()
  local fishRect =self.uiLayer:getFishRect()
  return self.AnimationLayer:passOne(fishRect)
end

function MainScene:isColliWithFish()
   local fishRect =self.uiLayer:getFishRect()
   return self.AnimationLayer:isColliWithFish(fishRect)
end


function MainScene:onEnter()
    if device.platform == "android" then
        -- avoid unmeant back
        self:performWithDelay(function()
            -- keypad layer, for android
            local layer = display.newLayer()
            layer:addKeypadEventListener(function(event)
                if event == "back" then app.exit() end
            end)
            self:addChild(layer)

            layer:setKeypadEnabled(true)
        end, 0.5)
    end
end

function MainScene:onExit()
end

return MainScene
